package com.lishu.carsafe.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.adapter.MonitorWarnAdapter2;
import com.lishu.carsafe.http.HttpBase;
import com.lishu.carsafe.http.HttpLoader;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.MonitorAlarmInfoModel;
import com.lishu.carsafe.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

/**
 * describe:
 *
 * @author:ZhangWei(张伟) date:2020/6/6
 * time:11:00
 * motto:高山仰止，景行行止，虽不能至，心向往之。
 */
public class MonitorWarnActivity extends BaseActivity {
    private final int COUNT_PER_PAGE = 10;//每页条数
    private SmartRefreshLayout refreshLayout;
    private RecyclerView rv_view;
    private MonitorWarnAdapter2 adapter;
    private int curPage = 1;

    private String vehicleId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor_warn);
        initView();
        initEven();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMonitorRealTimeAlarm();
    }

    private void initView() {
        setTopTitle("告警信息");
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        rv_view = findViewById(R.id.rv_view);
        adapter = new MonitorWarnAdapter2(this);
        rv_view.setLayoutManager(new LinearLayoutManager(this));
        rv_view.setAdapter(adapter);
    }

    private void initEven() {
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                curPage++;
                //这边开始请求数据
                getMonitorRealTimeAlarm();
                refreshLayout.finishLoadMore();
            }
        });
    }

    private void initData() {
        vehicleId = getIntent().getStringExtra(MonitorRealTimeActivity.MONITOR_VEHICLE_ID);
        getMonitorRealTimeAlarm();
    }

    private void getMonitorRealTimeAlarm(){
        HttpLoader.getMonitorRealTimeAlarm(vehicleId, curPage+"", COUNT_PER_PAGE+"", new HttpBase.IResponseListener() {
            @Override
            public void onResponse(BaseModel model) {
                hide();
                if (model == null) {
                    ToastUtil.showShort(R.string.please_check_network);
                    return;
                }
                if (!model.success()) {
                    ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                    return;
                }
                MonitorAlarmInfoModel infoModel = (MonitorAlarmInfoModel) model;
                adapter.setList(infoModel.getData());
            }
        });
    }

}
